<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="标题">
              <a-input placeholder="" v-model="queryParam.title" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"></a-divider>
          <a @click="handleGetDetail(record)">领取明细</a>
          <a-divider type="vertical"></a-divider>
          <a @click="handleExport(record)" :disabled="!record.bizStatusBool" :loading="exportLoading">导出二维码</a>
        </span>
        <span slot="bizStatusBool" slot-scope="text, record">
          <a-switch v-model="record.bizStatusBool" @change="handleStatus(record)" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <posMarketScanSence-modal ref="modalForm" @ok="modalFormOk"></posMarketScanSence-modal>
    <PosMarketSenceTaskList ref="modalForm1"></PosMarketSenceTaskList>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import PosMarketScanSenceModal from './modules/PosMarketScanSenceModal'
import PosMarketSenceTaskList from './modules/PosMarketSenceTaskList'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction, downloadFile } from '@/api/manage'
import moment from 'moment'

export default {
  name: 'PosMarketScanSenceList',
  mixins: [JeecgListMixin],
  components: {
    PosMarketScanSenceModal,
    PosMarketSenceTaskList,
  },
  data() {
    return {
      description: '扫码领券',
      isExport: false,
      moment,
      // 表头
      columns: [
        {
          title: '标题',
          align: 'center',
          dataIndex: 'title',
        },
        {
          title: '领取时间',
          align: 'center',
          dataIndex: 'startTime',
          customRender: (text, record) => {
            return record.startTime + ' 至 ' + record.endTime
          },
        },
        {
          title: '优惠券名称',
          align: 'center',
          dataIndex: 'couponName',
          customRender: (text, record) => {
            let senceDetailList = record.senceDetailList
            if (senceDetailList && senceDetailList.length > 0) {
              return senceDetailList[0].couponName
            } else {
              return ''
            }
          },
        },
        {
          title: '适用门店',
          align: 'center',
          dataIndex: 'scopeShopNames',
          customRender: (text, record) => {
            let senceShopList = record.senceShopList
            if (senceShopList && senceShopList.length > 0) {
              return senceShopList[0].scopeShopNames
            } else {
              return ''
            }
          },
        },
        {
          title: '每人限领',
          align: 'center',
          dataIndex: 'num',
          customRender: (text, record) => {
            let senceDetailList = record.senceDetailList
            if (senceDetailList && senceDetailList.length > 0) {
              return senceDetailList[0].num
            } else {
              return 0
            }
          },
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'bizStatusBool',
          scopedSlots: { customRender: 'bizStatusBool' },
        },
        {
          title: '操作人',
          align: 'center',
          dataIndex: 'creator',
        },
        {
          title: '操作时间',
          align: 'center',
          dataIndex: 'updateTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/marketing/sence/posMarketSence/list?senceType=10',
        changeBizStatusUrl: '/marketing/sence/posMarketSence/changeBizStatus',
        exportUrl: '/marketing/sence/posMarketSence/exportBatchQr',
      },
    }
  },
  computed: {},
  methods: {
    handleGetDetail(record) {
     this.$refs.modalForm1.view(record) 
     this.$refs.modalForm1.visible = true
    },
    handleStatus(record) {
      this.loading = true
      let toModify = {}
      toModify.id = record.id
      toModify.bizStatus = record.bizStatusBool ? 1 : 0
      postAction(this.url.changeBizStatusUrl, toModify)
        .then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
          } else {
            this.$message.warn(res.message)
          }
        })
        .finally(() => (this.loading = false))
    },
    async handleExport(record) {
      if(this.exportLoading){
        this.$message.warn('正在导出中...')
        return false
      }
      this.exportLoading = true
      let dateStr = moment().format('YYYYMMDD')
      await downloadFile(this.url.exportUrl, record.title + '_领券二维码_' + dateStr + '.zip', {id: record.id})
      this.exportLoading = false
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>